<?php

/**
 * @file
 *  Common functions for the SHMS
 */

function shms_user_with_super_access(){
  global $user;
  return user_access(SHMSP_PRESIDENT) || ($user->uid == 1);
}
 
function shms_add_default_value_for_fields(&$form,$valueArr,$setDisable=FALSE){
  foreach($form as $key=>&$value){
    if($key{0} == '#')  continue;
    if(array_key_exists($key, $valueArr)){
      $value['#default_value'] = $valueArr[$key];
    } else if(is_array($value)){
      shms_add_default_value_for_fields($value, $valueArr);
    }
    
    if($setDisable){
      $value['#disabled'] = TRUE;
    }
  }
}


/**
 * @param dataArr
 *  - The original array containing data
 * @param structureArr
 *  - The associated array defined like this:
 *      key1 => array(akey1, akey2, ...)
 *      key2 => ...
 * @return
 *  - An associated array defined by the structure array
 */
function shms_sort_array_data_into_subarr($dataArr,$structureArr){
  $res = array();
  foreach($structureArr as $key => $wantKey){
    $res[$key] = array();
    foreach ($dataArr as $oldKey => $ele) {
      if(in_array($oldKey, $wantKey)) $res[$key][$oldKey] = $dataArr[$oldKey];
    }
  }
  return $res;
}
 
/**
 * Get the current academic year. Assume the change-of-year would happen at the
 * beginning of July. (i.e., 6th/Jun/2010 is regarded as in year 2009, while 
 *  1st/July/2010 is treated as in year 2010) 
 * 
 * TODO: add in checking for user confirmation of the academic year
 * 
 * @return - an integer stands for the current academic year
 */
function shms_get_current_academic_year(){
  // TODO: stub here right now. To be completed later.
  return 2012;
}

/**
 * Generate an array of year options, based on the $minus and $plus parameter
 * 
 * @param $minus - lower range -- number of years to minus from current year
 * @param $plus  - upper range -- number of years to plus from current year
 * 
 * @return  an array of year options starting from lower range to upper range.
 */
function shms_core_common_generate_year_options($minus,$plus){
  $current_year = shms_get_current_academic_year();
  $opt_arr = array();
  for($i = $current_year-$minus; $i < $current_year+$plus; $i++){
    $opt_arr[strval($i)] = strval($i);
  }
  return $opt_arr;
}

/**
 * @return
 *  the current list of blocks as an array
 */
function shms_core_common_get_block_list(){
  return array(
    'A' => 'Blk A',
    'B' => 'Blk B',
    'C' => 'Blk C',
    'D' => 'Blk D',
    'E' => 'Blk E',
  );
}


function shms_core_get_date_arr_from_timestamp($timestamp){
  $date = explode(',', date("d,n,Y",$timestamp));
  return array(
    'day'  => $date[0],
    'month'  => $date[1],
    'year'  => $date[2],
  );
}

// validate if input is a valid number  
function shms_core_form_validate_number_field($element){
  if(empty($element['#value'])) return; // can leave it empty
  if(!is_numeric($element['#value'])) form_error($element,t("You can only put numbers in the CCA points field, but you put:".$element['#value']));
}

// validate if input is a valid number  
function shms_core_form_validate_int_field($element){
  if(empty($element['#value'])) return; // can leave it empty
  if(!preg_match("/^[0-9]+$/",$element['#value'])) form_error($element,t("You can only put integer here, but you put:".$element['#value']));
}

/**
 * Load the chairperson info, return it as a string to present
 * @param - array with person information
 */
function shms_put_person_str_from_info($person){
  return $person['full_name']." - ".$person['matric_no'];
}

function shms_validate_person_is_user($uid = NULL, $pid = NULL){
  global $user;
  if($uid == NULL && $pid == NULL)  return FALSE;
  if($uid == NULL){
    $person = shms_core_db_load_from_table(SHMS_PERSON_TABLE, 'id', $pid);
    $uid = $person['duid'];
  }
  return $uid == $user->uid;
}

function shms_validate_group_access($uid = NULL,$pid = NULL){
  global $user;
  if($user->uid == 0 || user_access(SHMSP_PRESIDENT)) return TRUE;
  return shms_validate_person_is_user($uid,$pid);
}

function shms_access($str){
  if(!user_access($str))  return FALSE;
  
  // then verify if the user_access filled
  global $user;
  // load user via duid
  $person = db_select(SHMS_PERSON_TABLE,"p")->condition("duid",$user->uid)
            ->fields('p')->execute()->fetchAssoc();
  if(empty($person))  return TRUE;  // don't care if no record
  if(!$person['personal_detail_filled']){
    drupal_set_message("You haven't key in your personal details yet. Please go to ".
      l("this page","person/my")." to update your details first.",'error',FALSE);
    return FALSE;
  }
  return TRUE;
}
